﻿package com.quickdev.intelligenthome.controls {
	
	import flash.display.MovieClip;
	import com.quickdev.intelligenthome.event.DateSelectedEvent;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	
	public class Calendar extends MovieClip {
		public static const TYPE_DAY :uint = 0;
		public static const TYPE_MONTH :uint = 1;
		
		public static const MONTH_DAYS :Array = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		public static const LEAP_MONTH_DAYS :Array = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		
		public var dateType :uint = TYPE_DAY;
		
		private var _date :Date = null;
		private var _selectedDate :Date = null;
		private var preSelectedDBtn :DateButton = null;
		private var monthDays :uint = 0;
		
		public function Calendar() {
			trace("Construct Calendar");
			_date = new Date();
			_selectedDate = new Date(_date.fullYear, _date.month, _date.date);
			addEventListener(DateSelectedEvent.DATE_SELECTED, onDateSelected);
		}
		
		private function onDateSelected( event :DateSelectedEvent ) :void {
			_date.setDate( event.selectedDate );
			_selectedDate.setFullYear( _date.fullYear, _date.month, _date.date );
			//event.stopImmediatePropagation();
			updateCalendar();
		}
		
		override public function toString() :String {
			var str :String = _selectedDate.fullYear + "-";
			var m :uint = _selectedDate.month + 1;
			if( m > 9 ) {
				str += m;
			}
			else {
				str += "0" + m;
			}
			
			if( dateType == TYPE_DAY ) {
				str += "-";
				if( _selectedDate.date > 9 ) {
					str += _selectedDate.date;
				}
				else {
					str += "0" + _selectedDate.date;
				}
			}
			
			return str;
		}
		
		public function clear() :void {
			for(var i :uint = 0; i < 6; ++i) {
				for(var j :uint = 0; j < 7; ++j) {
					this["dateBtn" + i + j].visible = false;
				}
			}
		}
		
		public function updateCalendar() :void {
			clear();
			
			var tempDate :Date = new Date(_date.fullYear, _date.month, 1);
			
			if( isLeapYear( tempDate.fullYear ) ) {
				monthDays = LEAP_MONTH_DAYS[ tempDate.month ];
			}
			else {
				monthDays = MONTH_DAYS[ tempDate.month ];
			}
			
			var i :uint = 0;
			var j :uint = 0;
			var count :uint = 1;
			
			j = tempDate.day;
			for( ; j < 7; ++j ) {
				this["dateBtn" + i + j].btn.label = count;
				this["dateBtn" + i + j].visible = true;
				++count;
			}
			
			for( i = 1; i < 6; ++i ) {
				for( j = 0; j < 7; ++j ) {
					this["dateBtn" + i + j].btn.label = count;
					this["dateBtn" + i + j].visible = true;
					++count;
					if( count > monthDays ) {
						break;
					}
				}
				if( count > monthDays ) {
					break;
				}
			}
			
			var index :uint = _date.date + tempDate.day - 1;
			i = index / 7;
			j = index % 7;
			this["dateBtn" + i + j].btn.selected = true;
			if( preSelectedDBtn ) {
				preSelectedDBtn.btn.selected = false;
			}
			preSelectedDBtn = this["dateBtn" + i + j];
			
			yearLabel.text = _date.fullYear + "年";
			monthLabel.text = (_date.month + 1) + "月";
		}
		
		public function setDate( date :Date ) :void {
			_date = new Date( date.fullYear, date.month, date.date );
			
			updateCalendar();
		}
		
		private function isLeapYear( year :uint ) :Boolean {
			if( year % 100 == 0 ) {
				if( year % 400 == 0 ) {
					return true;
				}
			}
			else if( year % 4 == 0 ) {
				return true;
			}
			
			return false;
		}
		
		private function addEvent() :void {
			nextMonthBtn.addEventListener(MouseEvent.CLICK, onNextMonthClick);
			nextYearBtn.addEventListener(MouseEvent.CLICK, onNextYearClick);
			preMonthBtn.addEventListener(MouseEvent.CLICK, onPreMonthClick);
			preYearBtn.addEventListener(MouseEvent.CLICK, onPreYearClick);
		}
		
		private function onNextMonthClick( event :MouseEvent ) :void {
			if( _date.month == 11 ) {
				_date.setFullYear( _date.fullYear + 1, 0, 1 );
			}
			else {
				_date.setMonth( _date.month + 1, 1 );
			}
			
			yearLabel.text = _date.fullYear + "年";
			monthLabel.text = (_date.month + 1) + "月";
			
			gotoAndPlay(15);
		}
		
		private function onNextYearClick( event :MouseEvent ) :void {
			_date.setFullYear( _date.fullYear + 1, _date.month, 1 );
			
			yearLabel.text = _date.fullYear + "年";
			monthLabel.text = (_date.month + 1) + "月";
			
			gotoAndPlay(15);
		}
		
		private function onPreMonthClick( event :MouseEvent ) :void {
			if( _date.month == 0 ) {
				_date.setFullYear( _date.fullYear - 1, 11, 1 );
			}
			else {
				_date.setMonth( _date.month - 1, 1 );
			}
			
			yearLabel.text = _date.fullYear + "年";
			monthLabel.text = (_date.month + 1) + "月";
			
			gotoAndPlay(28);
		}
		
		private function onPreYearClick( event :MouseEvent ) :void {
			_date.setFullYear( _date.fullYear - 1, _date.month, 1 );
			
			yearLabel.text = _date.fullYear + "年";
			monthLabel.text = (_date.month + 1) + "月";
			
			gotoAndPlay(28);
		}
		
		private function setLabelStyle() :void {
			var textFormat :TextFormat = new TextFormat();
			textFormat.size = 14;
			
			yearLabel.setStyle("textFormat", textFormat);
			monthLabel.setStyle("textFormat", textFormat);
		}
	}
	
}
